import 'dart:convert';
import 'package:shared_preferences/shared_preferences.dart';
//缓存工具类

class StorageUtil {
  static SharedPreferences sp;

  static Future<bool> setDouble(key, value) {
    return sp.setDouble(key, value);
  }

  static double getDouble(key) {
    if (!sp.containsKey(key)) {
      return 0.0;
    }
    return sp.getDouble(key) ?? 0.0;
  }

  static Future<bool> setInt(key, value) {
    return sp.setInt(key, value);
  }

  static int getInt(key) {
    if (!sp.containsKey(key)) {
      return 0;
    }
    return sp.getInt(key) ?? 0;
  }

  static Future<bool> setString(key, value) {
    return sp.setString(key, value);
  }

  static String getString(key) {
    if (!sp.containsKey(key)) {
      return "";
    }
    return sp.getString(key) ?? "";
  }

  static Future<bool> setList(key, value) {
    return sp.setStringList(key, value);
  }

  static List<String> getList(key) {
    if (!sp.containsKey(key)) {
      return [];
    }
    return sp.getStringList(key) ?? [];
  }

  static Future<bool> setMap(key, value) {
    return sp.setString(key, json.encode(value));
  }

  static Map getMap(key) {
    if (sp.containsKey(key)) {
      return json.decode(sp.getString(key) ?? "{}");
    } else {
      return {};
    }
  }

  static Future<bool> remove(key) {
    return sp.remove(key);
  }

  static Future<bool> clear() {
    return sp.clear();
  }
}
